@extends('manage.layout')

@section('content')
    <section class="content-header">
        <h1>
            图片管理
            <small>Preview</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="#">Case</a></li>
            <li class="active">Case Picture</li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <!-- left column -->
            <div class="col-md-12">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">上传图片</h3>
                    </div>
                    <form action="{{ url('case/picture') }}" method="post" role="form" style="max-width: 800px;" enctype="multipart/form-data" id="picture_form" onsubmit="return uploader()">
                        {{csrf_field()}}
                        <input type="hidden" id="picture_id" name="picture_id" value="{{ $case->id }}">
                        <div class="box-body" style="width: 800px;">
                            {{--<h4>您所选择的文件列表：</h4>--}}
                            {{--<div id="ossfile">你的浏览器不支持flash,Silverlight或者HTML5！</div>--}}
                            <div class="form-group">
                                <label for="exampleInputEmail1">权重</label>
                                <input type="number" id="picture_rank" name="rank" class="form-control" min="0" placeholder="rank" value="0">
                            </div>
                            <br/>
                            {{--<div id="container">--}}
                            {{--<a id="selectfiles" href="javascript:void(0);" class='btn btn-primary'>选择文件</a>--}}
                            {{--<a id="postfiles" href="javascript:void(0);" class='btn btn-primary'>开始上传</a>--}}
                            {{--</div>--}}
                            <span id="attachment" style="display: none">case/attachment/</span>
                            <span id="picture" style="display: none">case/picture/</span>
                            <div class="form-group">
                                <label for="exampleInputPassword1">图片</label>
                                <div id="container">
                                    <a id="selectfiles" href="javascript:void(0);" class='btn btn-primary'>选择文件</a>
                                    {{--<a id="postfiles" href="javascript:void(0);" class='btn btn-primary'>开始上传</a>--}}
                                </div>
                                <div class='clear container'>
                                    <!--<div id="dd" class="row"></div>-->
                                    <div id="ossfile"></div>
                                </div>
                                <div class='clear container' >
                                    <div id="img"></div>
                                </div>
                                <input type="hidden" name="path" value="" id="getpath">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">描述</label>
                                {{--<input type="text" name="url" class="form-control" id="exampleInputPassword1" placeholder="URL">--}}
                                <textarea id="picture_brief" name="brief" class="form-control"  placeholder="Brief"></textarea>
                            </div>


                            {{--<pre id="console"></pre>--}}

                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer">
                            <button type="submit" class="btn btn-primary">提交</button>
                        </div>
                    </form>

                    <!-- /.box-header -->
                    <!-- form start -->
                </div>
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">图片</h3>
                    </div>
                    <div class="box-body">
                        @if(empty($picture))还未上传图片
                        @else
                            @foreach($picture as $p)
                                <div style="margin: 10px;float: left;text-align: center">
                                    权重：<input type="number" id="setRank{{ $p['id'] }}" onblur="setRank({{ $p['id'] }})" value="{{ $p['rank'] }}" style="margin-bottom: 25px; width: 150px;">
                                    <p style="width: 250px">
                                        <div class="md-trigger" data-modal="modal-{{ $p['id'] }}" style="margin: auto">
                                            <img src="http://vipyipoint.oss-cn-beijing.aliyuncs.com/{{ $p['path'] }}" width="200px" height="200px">
                                        </div>
                                        {{--<img src="{{ $p['path'] }}" width="100px" height="100px">--}}
                                        <a href="javascript:delpicture('{{ $p['id'] }}','{{ $p['path'] }}')"><div class="fa fa-fw fa-remove" style="background-color:#fff;display:inline-block;width: 18px;height: 18px;border:1px solid #3c8dbc;border-radius: 9px;position: relative;top: -210px;right: -100px;z-index: 666;"></div></a><br />

                                        <textarea id="setContent{{ $p['id'] }}" onblur="setContent({{ $p['id'] }})" style="border:none;width: 200px;" rows="8" cols="30" name="content">{{ $p['content'] }}</textarea>
                                    </p>
                                    @if($p['status'] == 1)
                                        <a href="javascript:pictureStatus('{{ $p['id'] }}',0)" class='btn btn-warning btn-xs'>禁用</a>
                                    @else
                                        <a href="javascript:pictureStatus('{{ $p['id'] }}',1)" class='btn btn-success btn-xs'>启用</a>
                                    @endif
                                </div>
                                <div class="md-container md-effect-1" id="modal-{{ $p['id'] }}">
                                    <div class="md-content" style="width:600px;padding-top: 10px;text-align: center">
                                        <div style="margin:auto;color:#000;"><img src="http://vipyipoint.oss-cn-beijing.aliyuncs.com/{{ $p['path'] }}" width="500px" height="500px"></div>
                                        <div style="text-align: center">
                                            {{--<strong>文字:</strong><span>{{ $p['content'] }}</span>--}}
                                            <button class="md-close" style="margin-top: 10px;"><span style="color: #000;">关闭</span></button>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </section>
@stop
@section('script')
    <script type="text/javascript" src="/oss/upload_pic.js"></script>
    <script>

        $(document).ready(function() {

            $('.md-trigger').on('click',function(){

                var modal = $(this).data('modal');
                $("#" + modal).niftyModal();
            });

        });

        //修改图片描述
        function setContent(_id){
            var _content = $('#setContent'+_id).val();
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                url: '/case/picture/setContent',
                type: "POST",
                data: {content:_content,id:_id},
                dataType: "json",
                success: function(res) {
                    console.log(res);
                    if(res.code == 100) {
                        $('#setContent'+_id).val(_content);
                    }
                    else {
                    }
                },
                error:function(){
                    layer.msg('ERROR');
                }
            });
        }

        //修改图片权重
        function setRank(_id){
            var _rank = $('#setRank'+_id).val();
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                url: '/case/picture/setRank',
                type: "POST",
                data: {rank:_rank,id:_id},
                dataType: "json",
                success: function(res) {
                    console.log(res);
                    if(res.code == 100) {
                        $('#setRank'+_id).val(_rank);
                    }
                    else {
                    }
                },
                error:function(){
                    layer.msg('ERROR');
                }
            });
        }

        $(function() {
            //修改描述
//            $('#setContent').blur(function(){
//
//                alert(this);
//            });

            //上传图片
            $("form").submit(function(event) {
                event.preventDefault();
                var url = $(this).attr("action");
                var data = $("form").serialize();
                $.ajax({
                    url: url,
                    type: "POST",
                    data: data,
                    dataType: "json",
                    success: function(res) {
                        console.log(res);
                        if(res.code == 100) {
//                            window.location.href = "/caseList";
                            location.reload();
                            layer.msg(res.msg)
                        }
                        else {
                            location.reload();
                            layer.msg(res.msg)
                        }
                    }
                });
            });
        });

        //删除图片
        function delpicture(id,path) {
            layer.confirm('删除后不可恢复，是否继续?', {
                btn:['确定','取消']
            },function() {
                $.ajax({
                    url: '/case/deletePicture',
                    type: "GET",
                    data: {id:id,path:path},
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        if(data.code == 100) {
                            window.location.href = window.location.href;
                            layer.msg(data.msg)
                        }
                        else {
                            layer.msg(data.msg)
                        }
                    }
                });
            },function() {

            });
        }

        //禁用,启用
        function pictureStatus(id,status) {
            $.ajax({
                url: '/case/pictureStatus',
                type: "GET",
                data: {id:id,status:status},
                dataType: "json",
                success: function(res) {
                    console.log(res);
                    if(res.code == 100) {
                        window.location.href=window.location.href;
//                        if($('#setStatus').html() == '禁用'){
//                            $('#setStatus').addClass('btn btn-success btn-xs');
//                        }else{
//                            $('#setStatus').addClass('btn btn-danger btn-xs');
//                        }
                        layer.msg(res.msg, {time:1500, icon:6})
                    }
                    else {
                        layer.msg(res.msg, {time:1500, icon:5})
                    }
                }
            });
        }
    </script>
@stop
